package com.tz.springmvc.sysmanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.springmvc.sysmanage.dto.UserDto;
import com.tz.springmvc.sysmanage.entity.User;
import com.tz.springmvc.sysmanage.service.IUserService;
 
/**
 * 用注解的方式开发用户的增删改查功能
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sysmanage/user")
public class UserController{
 	@Autowired
	private IUserService userService;
	//查询用户列表
	@RequestMapping("/getUserList")
	public ModelAndView getUserList(){
		System.out.println("进入getUserList");
		ModelAndView modelAndView  = new ModelAndView();
		List<User> userList = userService.getUserList(new User());
 		//将数据放入modelAndView对象
 		modelAndView.addObject("userList",userList);
 		//讲返回的逻辑名称放入modelAndView对象
 		modelAndView.setViewName("sysmanage/user/userMain");
 		
		return modelAndView;
	}
	
	@RequestMapping("/getUserListAjaxJson")
	public @ResponseBody List<User> getUserListAjaxJson(){
		List<User> userList = userService.getUserList(new User());
		return userList;
	}
	
	//进入用户管理页面
	@RequestMapping("/gotoUser")
	public String gotoUser(){
		return "sysmanage/user/userMain";		
	}
	
	//进入用户新增或者修改页面
	@RequestMapping("/gotoUserEdit")
	public String gotoUserEdit(Long userId,Model model){	
		if(userId!=null){//进入的是修改页面
			User user = new User();
			user = userService.getUserById(userId);
			model.addAttribute("user", user);
		}
		return "sysmanage/user/userEdit";		
	}
	
	@RequestMapping("/getUserByIdAjaxJson")
	public @ResponseBody User getUserByIdAjaxJson(Long userId){
		User user = new User();
		user = userService.getUserById(userId);
		return user;
	}
	
	@RequestMapping("/userSave")
	public String userSave(User user,Model model){			
		if(user.getUserId()!=null){//修改数据
			if(userService.updateUser(user)){
				model.addAttribute("updateFlag","修改用户成功");
			}else{
				model.addAttribute("updateFlag","修改用户失败");
			}
		}else{//新增数据
			if(userService.addUser(user)){
				model.addAttribute("updateFlag","增加用户成功");
			}else{
				model.addAttribute("updateFlag","增加用户失败");
			}
		}	
		return "sysmanage/user/userEdit";		
	}
	
	@RequestMapping("/userSaveAjaxJson")
	public @ResponseBody Map<String,Object> userSaveAjaxJson(@RequestBody User user){		
		 Map<String,Object> resultMap = new HashMap<String,Object>();
		 if(user.getUserId()!=null){
			if(userService.updateUser(user)){
				resultMap.put("result", "修改用户成功") ; 
			}else{
				resultMap.put("result", "修改用户失败");  
			}	
		 }else{
			 if(userService.addUser(user)){
					resultMap.put("result", "新增成功") ; 
				}else{
					resultMap.put("result", "新增用户失败");  
				} 
		 }
		return resultMap;
	}
	
	
	
	@RequestMapping("/userDel")
	public String userDel(Long userId,Model model){				 
		if(userId!=null){
			if(userService.delUser(userId)){
				model.addAttribute("updateFlag","删除用户成功");
			}else{
				model.addAttribute("updateFlag","删除用户失败");
			}
		}		 
		return "redirect:/sysmanage/user/getUserList.do";		
	}
	
	@RequestMapping("/userDelAjaxJson")
	public @ResponseBody Map<String,Object> userDelAjaxJson(Long userId){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
		if(userId!=null){
			if(userService.delUser(userId)){
				resultMap.put("result", "删除用户成功") ; 
			}else{
				resultMap.put("result", "删除用户失败");  
			}
		}		 
		return resultMap;
	}
  
	@RequestMapping("/userUpdateBatch")
	public String userUpdateBatch(UserDto userDto,Model model){
		if(userDto.getUserList()!=null&&userDto.getUserList().size()>0){
			if(userService.updateUserBatch(userDto.getUserList())){
				model.addAttribute("updateFlag","批量修改用户成功");
			}else{
				model.addAttribute("updateFlag","批量修改用户失败");
			}
		}	
		return "redirect:/sysmanage/user/getUserList.do";		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
